package com.cdut.hwadeefsc.controller;

import com.cdut.hwadeefsc.entity.homework;
import com.cdut.hwadeefsc.service.homeworksService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/homework")
public class homeworkController {

    @Autowired
    private homeworksService homeworkService;

    // 发布作业（任课老师权限）
    @PostMapping("/publish")
    public ResponseEntity<?> publishHomework(@RequestBody homework homework) {
        try {
            boolean success = homeworkService.addHomework(homework);
            return success ? ResponseEntity.ok().build() : ResponseEntity.badRequest().build();
        } catch (Exception e) {
            return ResponseEntity.internalServerError().build();
        }
    }

    // 为指定班级发布作业（任课老师权限）
    @PostMapping("/class/{classId}/publish")
    public ResponseEntity<?> publishClassHomework(@PathVariable Integer classId, @RequestBody homework homework) {
        try {
            homework.setClassId(classId);
            boolean success = homeworkService.addHomework(homework);
            return success ? ResponseEntity.ok().build() : ResponseEntity.badRequest().build();
        } catch (Exception e) {
            return ResponseEntity.internalServerError().build();
        }
    }

    // 查看班级作业列表
    @GetMapping("/class/{classId}")
    public ResponseEntity<List<homework>> getClassHomework(@PathVariable Integer classId) {
        try {
            List<homework> homeworkList = homeworkService.getHomeworkByClassId(classId);
            return ResponseEntity.ok(homeworkList);
        } catch (Exception e) {
            return ResponseEntity.internalServerError().build();
        }
    }

    // 查看学生个人作业（通过学生ID）
    @GetMapping("/student/{studentId}")
    public ResponseEntity<List<homework>> getStudentHomework(@PathVariable Integer studentId) {
        try {
            List<homework> homeworkList = homeworkService.getHomeworkByStudentId(studentId);
            return ResponseEntity.ok(homeworkList);
        } catch (Exception e) {
            return ResponseEntity.internalServerError().build();
        }
    }

    // 根据ID查看作业详情
    @GetMapping("/{homeworkId}")
    public ResponseEntity<homework> getHomeworkById(@PathVariable Integer homeworkId) {
        try {
            homework homework = homeworkService.getHomeworkById(homeworkId);
            if (homework != null) {
                return ResponseEntity.ok(homework);
            } else {
                return ResponseEntity.notFound().build();
            }
        } catch (Exception e) {
            return ResponseEntity.internalServerError().build();
        }
    }

    // 更新作业信息
    @PutMapping("/{homeworkId}")
    public ResponseEntity<?> updateHomework(@PathVariable Integer homeworkId, @RequestBody homework homework) {
        try {
            homework.setId(homeworkId);
            boolean success = homeworkService.updateHomework(homework);
            return success ? ResponseEntity.ok().build() : ResponseEntity.badRequest().build();
        } catch (Exception e) {
            return ResponseEntity.internalServerError().build();
        }
    }

    // 删除作业
    @DeleteMapping("/{homeworkId}")
    public ResponseEntity<?> deleteHomework(@PathVariable Integer homeworkId) {
        try {
            boolean success = homeworkService.deleteHomework(homeworkId);
            return success ? ResponseEntity.ok().build() : ResponseEntity.badRequest().build();
        } catch (Exception e) {
            return ResponseEntity.internalServerError().build();
        }
    }

    // 根据课程名称筛选作业
    @GetMapping("/course/{courseName}")
    public ResponseEntity<List<homework>> getHomeworkByCourse(@PathVariable String courseName) {
        try {
            // 这里需要先在Service层实现按课程查询的方法
            List<homework> homeworkList = homeworkService.getHomeworkByCourse(courseName);
            return ResponseEntity.ok(homeworkList);
        } catch (Exception e) {
            return ResponseEntity.internalServerError().build();
        }
    }

    // 获取未过期的作业（截止日期大于当前时间）
    @GetMapping("/active")
    public ResponseEntity<List<homework>> getActiveHomework() {
        try {
            // 这里需要先在Service层实现获取未过期作业的方法
            List<homework> homeworkList = homeworkService.getActiveHomework();
            return ResponseEntity.ok(homeworkList);
        } catch (Exception e) {
            return ResponseEntity.internalServerError().build();
        }
    }
}
